home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / util / arc / xadmasterdev.lha / xad / Sources / clients / xadIO.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-20  |  6.2 KB  |  253 lines

  1. #ifndef XADMASTER_IO_C
  2. #define XADMASTER_IO_C
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        xadIO.c
  7.     Main:        xadmaster
  8.     Versionstring:    $VER: xadIO.c 1.3 (12.08.2002)
  9.     Author:        SDI
  10.     Distribution:    Freeware
  11.     Description:    input/output functions
  12.  
  13.  1.0   24.10.00 : first version, based on older seperate files
  14.  1.1   07.11.00 : moved xadIOPutChar, xadIOGetChar into structure to
  15.     support different functions
  16.  1.2   24.03.01 : added ReadBits versions
  17.  1.3   12.08.02 : now reads until real input end (in case of short buffer)
  18. */
  19.  
  20. /* In case you are calling this file directly from source, you may use this
  21. defines to make the functions static, inlined or whatever else. In case it
  22. should be used a sobject file, they must be cleared, as they default to
  23. static. */
  24.  
  25. /* the main functions */
  26. #ifndef XADIOFUNCMODE
  27. #define XADIOFUNCMODE static
  28. #endif
  29.  
  30. /* the bit functions */
  31. #ifndef XADIOFUNCMODEBITS
  32. #define XADIOFUNCMODEBITS XADIOFUNCMODE
  33. #endif
  34.  
  35. #define XADIODIRECTMODE
  36. #include "xadIO.h"
  37. #include <exec/memory.h>
  38.  
  39. #define XIDBUFSIZE        10240
  40.  
  41. static UBYTE xadIOPutFunc(struct xadInOut *io, UBYTE data)
  42. {
  43.   if(!io->xio_Error)
  44.   {
  45.     if(!io->xio_OutSize && !(io->xio_Flags & XADIOF_NOOUTENDERR))
  46.     {
  47.       io->xio_Error = XADERR_DECRUNCH;
  48.       io->xio_Flags |= XADIOF_ERROR;
  49.     }
  50.     else
  51.     {
  52.       if(io->xio_OutBufferPos >= io->xio_OutBufferSize)
  53.         xadIOWriteBuf(io);
  54.       io->xio_OutBuffer[io->xio_OutBufferPos++] = data;
  55.       if(!--io->xio_OutSize)
  56.         io->xio_Flags |= XADIOF_LASTOUTBYTE;
  57.     }
  58.   }
  59.   return data;
  60. }
  61.  
  62. static UBYTE xadIOGetFunc(struct xadInOut *io)
  63. {
  64.   UBYTE res = 0;
  65.  
  66.   if(!io->xio_Error)
  67.   {
  68.     if(!io->xio_InSize)
  69.     {
  70.       if(!(io->xio_Flags & XADIOF_NOINENDERR))
  71.       {
  72.         io->xio_Error = XADERR_DECRUNCH;
  73.         io->xio_Flags |= XADIOF_ERROR;
  74.       }
  75.     }
  76.     else
  77.     {
  78.       if(io->xio_InBufferPos >= io->xio_InBufferSize)
  79.       {
  80.         ULONG i;
  81.         struct xadMasterBase *xadMasterBase = io->xio_xadMasterBase;
  82.  
  83.         if((i = io->xio_InBufferSize) > io->xio_InSize)
  84.           i = io->xio_InSize;
  85.         if(!io->xio_ArchiveInfo)
  86.         {
  87.           io->xio_Flags |= XADIOF_ERROR;
  88.           io->xio_Error = XADERR_DECRUNCH;
  89.         }
  90.         else
  91.         {
  92.           ULONG j;
  93.           j = io->xio_ArchiveInfo->xai_InSize-io->xio_ArchiveInfo->xai_InPos;
  94.           if(i > j)
  95.             i = j;
  96.           if(!i)
  97.           {
  98.             io->xio_Flags |= XADIOF_ERROR;
  99.             io->xio_Error = XADERR_INPUT;
  100.           }
  101.           else if(!(io->xio_Error = xadHookTagAccess(XADAC_READ, i, io->xio_InBuffer, io->xio_ArchiveInfo,
  102.           XAD_USESKIPINFO, 1, TAG_DONE)))
  103.           {
  104.             if(io->xio_InFunc)
  105.               io->xio_InFunc(io, i);
  106.             res = *io->xio_InBuffer;
  107.           }
  108.           else
  109.             io->xio_Flags |= XADIOF_ERROR;
  110.         }
  111.         io->xio_InBufferPos = 1;
  112.       }
  113.       else
  114.         res = io->xio_InBuffer[io->xio_InBufferPos++];
  115.       --io->xio_InSize;
  116.     }
  117.     if(!io->xio_InSize)
  118.       io->xio_Flags |= XADIOF_LASTINBYTE;
  119.   }
  120.  
  121.   return res;
  122. }
  123.  
  124. XADIOFUNCMODE struct xadInOut *xadIOAlloc(ULONG flags, struct xadArchiveInfo *ai, struct xadMasterBase *xadMasterBase)
  125. {
  126.   ULONG size = sizeof(struct xadInOut);
  127.   struct xadInOut *io;
  128.  
  129.   if(flags & XADIOF_ALLOCINBUFFER)
  130.     size += XIDBUFSIZE;
  131.   if(flags & XADIOF_ALLOCOUTBUFFER)
  132.     size += XIDBUFSIZE;
  133.   if((io = (struct xadInOut *) xadAllocVec(size, MEMF_CLEAR|MEMF_PUBLIC)))
  134.   {
  135.     STRPTR b;
  136.  
  137.     b = (STRPTR) (io+1);
  138.     io->xio_Flags = flags;
  139.     io->xio_PutFunc = xadIOPutFunc;
  140.     io->xio_GetFunc = xadIOGetFunc;
  141.     io->xio_ArchiveInfo = ai;
  142.     io->xio_xadMasterBase = xadMasterBase;
  143.     if(flags & XADIOF_ALLOCINBUFFER)
  144.     {
  145.       io->xio_InBuffer = b; b += XIDBUFSIZE;
  146.       io->xio_InBufferSize = io->xio_InBufferPos = XIDBUFSIZE;
  147.     }
  148.     if(flags & XADIOF_ALLOCOUTBUFFER)
  149.     {
  150.       io->xio_OutBuffer = b;
  151.       io->xio_OutBufferSize = XIDBUFSIZE;
  152.     }
  153.   }
  154.   return io;
  155. }
  156.  
  157. #ifdef XADIOGETBITSLOW
  158. XADIOFUNCMODEBITS ULONG xadIOGetBitsLow(struct xadInOut *io, UBYTE bits)
  159. {
  160.   ULONG x;
  161.  
  162.   while(io->xio_BitNum < bits)
  163.   {
  164.     io->xio_BitBuf |= xadIOGetChar(io) << io->xio_BitNum;
  165.     io->xio_BitNum += 8;
  166.   }
  167.   x = io->xio_BitBuf & ((1<<bits)-1);
  168.   io->xio_BitBuf >>= bits;
  169.   io->xio_BitNum -= bits;
  170.   return x;
  171. }
  172. #endif
  173.  
  174. #ifdef XADIOREADBITSLOW
  175. XADIOFUNCMODEBITS ULONG xadIOReadBitsLow(struct xadInOut *io, UBYTE bits)
  176. {
  177.   while(io->xio_BitNum < bits)
  178.   {
  179.     io->xio_BitBuf |= xadIOGetChar(io) << io->xio_BitNum;
  180.     io->xio_BitNum += 8;
  181.   }
  182.   return io->xio_BitBuf & ((1<<bits)-1);
  183. }
  184.  
  185. XADIOFUNCMODEBITS void xadIODropBitsLow(struct xadInOut *io, UBYTE bits)
  186. {
  187.   io->xio_BitBuf >>= bits;
  188.   io->xio_BitNum -= bits;
  189. }
  190. #endif
  191.  
  192. #ifdef XADIOGETBITSHIGH
  193. XADIOFUNCMODEBITS ULONG xadIOGetBitsHigh(struct xadInOut *io, UBYTE bits)
  194. {
  195.   ULONG x;
  196.  
  197.   while(io->xio_BitNum < bits)
  198.   {
  199.     io->xio_BitBuf = (io->xio_BitBuf << 8) | xadIOGetChar(io);
  200.     io->xio_BitNum += 8;
  201.   }
  202.   x = (io->xio_BitBuf >> (io->xio_BitNum-bits)) & ((1<<bits)-1);
  203.   io->xio_BitNum -= bits;
  204.   return x;
  205. }
  206. #endif
  207.  
  208. #ifdef XADIOREADBITSHIGH
  209. XADIOFUNCMODEBITS ULONG xadIOReadBitsHigh(struct xadInOut *io, UBYTE bits)
  210. {
  211.   while(io->xio_BitNum < bits)
  212.   {
  213.     io->xio_BitBuf = (io->xio_BitBuf << 8) | xadIOGetChar(io);
  214.     io->xio_BitNum += 8;
  215.   }
  216.   return (io->xio_BitBuf >> (io->xio_BitNum-bits)) & ((1<<bits)-1);
  217. }
  218.  
  219. XADIOFUNCMODEBITS void xadIODropBitsHigh(struct xadInOut *io, UBYTE bits)
  220. {
  221.   io->xio_BitNum -= bits;
  222. }
  223. #endif
  224.  
  225. XADIOFUNCMODE LONG xadIOWriteBuf(struct xadInOut *io)
  226. {
  227.   if(!io->xio_Error && io->xio_OutBufferPos)
  228.   {
  229.     struct xadMasterBase *xadMasterBase = io->xio_xadMasterBase;
  230.  
  231.     if(io->xio_OutFunc)
  232.       io->xio_OutFunc(io, io->xio_OutBufferPos);
  233.     if(!(io->xio_Flags & XADIOF_COMPLETEOUTFUNC))
  234.     {
  235.       if(!io->xio_ArchiveInfo)
  236.       {
  237.         io->xio_Flags |= XADIOF_ERROR;
  238.         io->xio_Error = XADERR_DECRUNCH;
  239.       }
  240.       else if((io->xio_Error = xadHookTagAccess(XADAC_WRITE, io->xio_OutBufferPos,
  241.       io->xio_OutBuffer, io->xio_ArchiveInfo,
  242.       io->xio_Flags & XADIOF_NOCRC16 ? TAG_IGNORE : XAD_GETCRC16, &io->xio_CRC16,
  243.       io->xio_Flags & XADIOF_NOCRC32 ? TAG_DONE   : XAD_GETCRC32, &io->xio_CRC32,
  244.       TAG_DONE)))
  245.         io->xio_Flags |= XADIOF_ERROR;
  246.     }
  247.     io->xio_OutBufferPos = 0;
  248.   }
  249.   return io->xio_Error;
  250. }
  251.  
  252. #endif /* XADMASTER_IO_C */
  253.